package cn.example.demo.modules.house.controller;

import cn.example.demo.common.model.response.HttpDataResponseResult;
import cn.example.demo.common.model.response.HttpResponseResult;
import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.annotation.AuthEnable;
import cn.example.demo.common.validation.groups.Delete;
import cn.example.demo.common.validation.groups.Insert;
import cn.example.demo.common.validation.groups.Update;
import cn.example.demo.modules.house.dto.RecommendResultDto;
import cn.example.demo.modules.house.entity.RecommendResult;
import cn.example.demo.modules.house.recommend.task.RecommendGenerationTask;
import cn.example.demo.modules.house.service.IHouseService;
import cn.example.demo.modules.house.service.IRecommendResultService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 【推荐结果】控制器
 * </p>
 */
@Validated
@RestController
@RequestMapping("/api/RecommendResult")
@Api(tags = {"【推荐结果】操作接口"})
public class RecommendResultController {
    @Resource
    private IRecommendResultService recommendResultService;
    @Resource
    private RecommendGenerationTask recommendGenerationTask;

    @AuthEnable
    @PostMapping("/add")
    @ApiOperation(value = "新增【推荐结果】")
    public HttpResponseResult addRecommendResult(@ApiParam(value = "推荐结果请求参数体") @RequestBody @Validated(Insert.class) RecommendResultDto dto) {
        // 保存RecommendResult
        ServiceResult result = recommendResultService.insertRecommendResult(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("/modify")
    @ApiOperation(value = "修改【推荐结果】")
    public HttpResponseResult modifyRecommendResult(@ApiParam(value = "推荐结果请求参数体") @RequestBody @Validated(Update.class) RecommendResultDto dto) {
        // 保存RecommendResult
        ServiceResult result = recommendResultService.updateRecommendResult(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @GetMapping(value = "/retrieval")
    @ApiOperation(value = "查询【推荐结果】")
    public HttpResponseResult getRecommendResultList(@ApiParam(value = "推荐结果请求参数体") RecommendResultDto dto) {
        PageBean<RecommendResult> result = recommendResultService.queryRecommendResult(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【推荐结果】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @GetMapping(value = "/find/{id}")
    @ApiOperation(value = "根据 ID 查询单个【推荐结果】")
    public HttpResponseResult getOneRecommendResult(@ApiParam(value = "推荐结果ID") @PathVariable @NotBlank(message = "id不能为空！") String id) {
        RecommendResult result = recommendResultService.findRecommendResultById(id);

        if (result == null) {
            return HttpDataResponseResult.isNotFound(StrUtil.format("未查询到【推荐结果】记录！ID:{}", id), null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @DeleteMapping("remove")
    @ApiOperation(value = "删除【推荐结果】")
    public HttpResponseResult removeRecommendResult(@ApiParam(value = "推荐结果请求参数体") @RequestBody @Validated(Delete.class) RecommendResultDto dto) {
        ServiceResult result = recommendResultService.deleteRecommendResult(dto);

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @GetMapping(value = "/dictList")
    @ApiOperation(value = "以字典表格式查询【推荐结果】")
    public HttpResponseResult getRecommendResultAsDict(@ApiParam(value = "推荐结果请求参数体") RecommendResultDto dto) {
        List result = recommendResultService.queryRecommendResultAsDict(dto);
        if (ObjectUtil.isEmpty(result)) {
            return HttpDataResponseResult.isNotFound("【推荐结果】列表：没有数据！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @PutMapping(value = "/refresh/recommend")
    @ApiOperation(value = "刷新推荐结果")
    public HttpResponseResult refreshRecommendResult() {
        recommendGenerationTask.generateDailyRecommend();
        return HttpDataResponseResult.isSuccess(null);
    }
}
